var _currentIndex = 0;
var _index = 0;
var _rowIndex = 0;

efform_onload = function ()
{
	$('#ef_tab_y').hide();
}

/**
 * 点击主表记录
 */
efgrid_onRowClicked = function(grid_id, row_index) { 
	if (grid_id == "ef_grid_result") { 
		_rowIndex = row_index;
		$('#ef_tab_y').show();
		var grid = efgrid.getGridObject(grid_id);
		var fdProduceplanId = grid.getCellValueByColumnName(row_index,	"fdKeyId"); 
		var fdPlanStatus = grid.getCellValueByColumnName(row_index,	"fdPlanStatus"); 
		$('#fdProduceplanId').val(fdProduceplanId); 
		$('#fdPlanStatus').val(fdPlanStatus); 
		changeTab(_index);
	}
}

function changeTab(indexclicked)
{
	var year=$('#fdPlanYear').val();
	var month=$('#fdPlanMonth').val();
	var fdProduceplanId=$('#fdProduceplanId').val();
	var fdPlanStatus=$('#fdPlanStatus').val();

	if(indexclicked ==0)
	{  
		var frame = frames[0];
		frame.src = "DispatchAction.do?efFormEname=ZMCP02"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}else if(indexclicked ==1)
	{ 
		var frame = frames[1];
		frame.src = "DispatchAction.do?efFormEname=ZMCP11"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==2)
	{ 
		var frame = frames[2];
		frame.src = "DispatchAction.do?efFormEname=ZMCP13"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==3)
	{ 
		var frame = frames[3];
		frame.src = "DispatchAction.do?efFormEname=ZMCP15"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==4)
	{ 
		var frame = frames[4];
		frame.src = "DispatchAction.do?efFormEname=ZMCP31"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==5)
	{
		var frame = frames[5];
		frame.src = "DispatchAction.do?efFormEname=ZMCP41"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==6)
	{ 
		var frame = frames[6];
		frame.src = "DispatchAction.do?efFormEname=ZMCP51"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==7)
	{ 
		var frame = frames[7];
		frame.src = "DispatchAction.do?efFormEname=ZMCP61"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==8)
	{ 
		var frame = frames[8];
		frame.src = "DispatchAction.do?efFormEname=ZMCP71"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==9)
	{ 
		var frame = frames[9];
		frame.src = "DispatchAction.do?efFormEname=ZMCP81"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==10)
	{ 
		var frame = frames[10];
		frame.src = "DispatchAction.do?efFormEname=ZMCP91"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
	else if(indexclicked ==11)
	{ 
		var frame = frames[11];
		frame.src = "DispatchAction.do?efFormEname=ZMCP93"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}else if(indexclicked ==12)
	{ 
		var frame = frames[12];
		frame.src = "DispatchAction.do?efFormEname=ZMCP06"+"&fdPlanYear="+year+"&fdPlanMonth="+month+"&fdProduceplanId="+fdProduceplanId+"&fdPlanStatus="+encodeURI(encodeURI(fdPlanStatus));
		efform.setStatus(0, "正在加载...");
	}
}

function fundiv(currentIndex,index){  
	_currentIndex=currentIndex;
	_index=index;
	changeTab(index);
} 

//点击主表新增按钮
button_insert_onclick = function (){
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return;	
	efgrid.submitForm( "ef_grid_result", "ZM", "ZMCP03", "insert", true);
} 

button_update_onclick = function() {

	var grid = isSelectedOne("ef_grid_result");
	if(grid==0)return;
	
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")=="生效" || grid.getCellValueByColumnName(rows[i],"fdPlanStatus")=="历史")
		{
			EFAlert("只能对草拟状态的记录进行修改！","警告"); return;
		}
	}
	
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP03", "update", true);
}

button_adjust_onclick = function() { 
	var grid = isSelectedOne("ef_grid_result");
	if(grid==0)return; 

	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能调整一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="生效")
		{
			EFAlert("不能调整草拟或历史状态的记录！","警告"); return;
		}
	}
	
	EFConfirm('你确认要调整当前计划?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_result", "ZM", "ZMCP03", "adjust", true);
	});
}

button_report_onclick = function() { 
	var grid = isSelectedOne("ef_grid_result");
	if(grid==0)return;

	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能发布一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="草拟")
		{
			EFAlert("不能发布已生效或历史状态的记录！","警告"); return;
		}
	}

	EFConfirm('你确认要发布当前计划?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_result", "ZM", "ZMCP03", "report", true);
	});
}

refresh_main_grid = function() {
	var ajax_callback =
	{
		onSuccess :
    		function(eiInfo)
			{
				grid = efgrid.getGridObject("ef_grid_result");
				grid.refresh(eiInfo);
				grid.setCurrentRowIndex(_rowIndex);
    		},
  		onFail:
    		function(eMsg)
			{
   				alert("服务调用失败: " + eMsg);
			}
	};

	var info = new EiInfo();

	info.setById("fdPlanYear");
	info.setById("fdPlanMonth");
  	EiCommunicator.send( "ZMCP03", "query", info, ajax_callback );
}

//点击了新增行之后，给一些默认数据
//主表新增时，给默认数据
efgrid_afterAddNewRow = function(grid_id){
	var year=$('#fdPlanYear').val();
	var month=$('#fdPlanMonth').val();

	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	
	if(grid_id=="ef_grid_result")
	{
		for(var i = 0 ; i < rows.length;i++){
			grid.getBlockData().setCell(rows[i], "fdPlanStatus", "草拟");
			grid.getBlockData().setCell(rows[i], "fdTitle", year+"年"+month+"月的生产计划");
		}
	}
	for(var i = 0 ; i < rows.length;i++){
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
}
